#!/usr/bin/env sh

IS_PUBLISHED_VERSION=`git describe --tags --exact-match 2>/dev/null`

if [ -n "$CIRCLE_BUILD_NUM" ]; then
    if [ -n "$IS_PUBLISHED_VERSION" ]; then
        echo "export publish=true" > build.properties
    else
        echo "export publish=false" > build.properties
    fi
fi

if [ -z "$MB_VERSION" ]; then
    MB_VERSION=`cat package.json | grep '"version"' | sed -E -e 's/.*"version": "([^"]*)",/\1/'`
fi

# We don't set the version suffix on tagged (published) builds
if [ -n "$CIRCLE_BUILD_NUM" -a -z "$IS_PUBLISHED_VERSION" ]; then
    MB_VERSION="$MB_VERSION-beta.$CIRCLE_BUILD_NUM"
fi

npm ci
MB_VERSION=$MB_VERSION MB_SLOW_TEST_TIMEOUT=6000 MB_PORT=3535 node_modules/grunt-cli/bin/grunt "$@"
